package com.qf.dao.impl;

import com.qf.dao.ProductDao;
import com.qf.entity.Product;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author:YuhuaZhang
 * @Description
 * @Date:Created in 10:01 2021/11/5
 * @Modified By:
 */
public class ProductDaoImpl implements ProductDao {
    @Override
    public long selectCountByTid(String tid) throws SQLException {

        QueryRunner queryRunner=new QueryRunner();

        String sql="select count(1) from product where t_id=?";

        Object result=queryRunner.query(DBUtil.getConnection(),sql,new ScalarHandler<>(),tid);

        Long total= (Long) result;
        return total;
    }

    @Override
    public List<Product> selectProductByPage(int page, int pageSize, String tid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();

        String sql="select p_id as pid,t_id as tid,p_name as pname,p_time as ptime," +
                "p_image as pimage,p_state as pstate,p_info as pinfo,p_price as pprice " +
                "from product where t_id=? limit ?,?;";

        List<Product> list = queryRunner.query(DBUtil.getConnection(), sql, new BeanListHandler<Product>(Product.class),
                tid, (page - 1) * pageSize, pageSize);
        return list;
    }

    @Override
    public Product selectProductByPid(String pid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();

        String sql="select p_id as pid,t_id as tid,p_name as pname,p_time as ptime," +
                "p_image as pimage,p_state as pstate,p_info as pinfo,p_price as pprice " +
                "from product where p_id=?;";

        Product product = queryRunner.query(DBUtil.getConnection(), sql, new BeanHandler<Product>(Product.class), pid);
        return product;
    }
}
